package com.lucun.es_transaction.controller;


import com.alipay.api.AlipayApiException;
import com.lucun.es_transaction.config.AlipayConfig;
import com.lucun.es_transaction.config.RolePermission;
import com.lucun.es_transaction.service.AlipayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;


@Controller
@RequestMapping("/alipay")
public class AlipayController {
    @Autowired
    private AlipayService alipayService;

    //创建订单提交订单
    @RolePermission(login = false)
    @RequestMapping("/topay")
    public String topay() {
        return "index";
    }

    //支付失败
    @RolePermission(login = false)
    @ResponseBody
    @RequestMapping("/failure")
    public String topay1() {
        return "支付失败";
    }

    //支付成功
    @RolePermission(login = false)
    @ResponseBody
    @RequestMapping("/success")
    public String topay2() {
        return "支付成功";
    }

//支付数据 HTML
    @RolePermission(login = false)
    @ResponseBody
    @RequestMapping("/pay")
    public String payController(@RequestParam("WIDout_trade_no") String id, @RequestParam("WIDsubject") String name, @RequestParam("WIDtotal_amount") String amount) throws Exception {
        Integer pay = Integer.valueOf(amount);
        return alipayService.webPagePay(id, name, pay);
    }

    //支付 数据写死
    @RolePermission(login = false)
    @ResponseBody
    @RequestMapping("/pay1")
    public String toPay(HttpServletResponse response, HttpServletRequest request) throws IOException, AlipayApiException {
        String pays = alipayService.pay(response, request);
//        支付成功添加日志
        AlipayConfig.logResult(pays);
        return pays;
    }

    //    退款
    @RolePermission(login = false)
    @ResponseBody
    @RequestMapping("/refund")
    public String toRefund(HttpServletResponse response, HttpSession session) throws IOException, AlipayApiException {
        String pay = alipayService.refund1(response, session);
//        退款成功添加日志
        AlipayConfig.logResult(pay);
        return pay;
    }
}
